package com.testDemo.hutool;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.*;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;

public class JSONUtilTest {
    @Test
    public void parseArray(){
        System.out.println(JSONUtil.parseArray("[]").size());//0
        System.out.println(JSONUtil.parseArray("[{}]").size());//1
        System.out.println(JSONUtil.parseArray("{}").size());//cn.hutool.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]

    }
    @Test
    public void setNULL(){
        JSONObject logMap = new JSONObject(JSONConfig.create().setIgnoreNullValue(false));
        logMap.set("aaa",null);
        System.out.println(logMap.toString());//0

    }

    @Test
    public void isTypeJSONArray(){

        System.out.println(  JSONUtil.isTypeJSONArray(null));//false
        System.out.println(  JSONUtil.isTypeJSONArray("{}"));//false
        System.out.println(  JSONUtil.isTypeJSONArray("[]"));//true
        System.out.println(  JSONUtil.isTypeJSONArray("[{a:1}]"));//true
        System.out.println(  JSONUtil.isTypeJSONArray("[{}]"));//true
    }

    @Test
    public void removeKeysTest(){
        JSON json = JSONUtil.parse("[[\"CHINA-CHINA\"]]");
        removeKeys(json);
    }
    public void removeKeys(JSON json) {
        Set<String> re = new HashSet<>();
        if (json != null) {
            if (json instanceof JSONArray) {
                JSONArray jsonArray = (JSONArray) json;
                if (CollectionUtil.isNotEmpty(jsonArray)) {
                    int len  =jsonArray.size();
                    Object row;
                    for (int i = 0; i < len; i++) {
                        row = jsonArray.get(i);
                        if (row instanceof JSONArray) {
                            removeKeys((JSONArray) row);
                        } else if (row instanceof cn.hutool.json.JSONObject) {
                            removeKeys((cn.hutool.json.JSONObject) row);
                        }else {
                            jsonArray.set(i,"*");
                        }
                    }
                }
            } else {
                cn.hutool.json.JSONObject jsonObject = (cn.hutool.json.JSONObject) json;
                jsonObject.keySet().forEach(row -> {
                    Object value = jsonObject.get(row);
                    if (value instanceof JSONArray) {
                        removeKeys((JSONArray) value);
                    } else if (value instanceof cn.hutool.json.JSONObject) {
                        removeKeys((cn.hutool.json.JSONObject) value);
                    } else if(StringUtils.isNotBlank(value.toString())){
                        jsonObject.set(row, "*");
                    }
                });
            }

        }
    }
    @Test
    public void test(){
        String json ="[\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 5,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 5\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 6,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 6\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 7,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 7\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 8,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 8\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 9,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 9\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 10,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 10\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 11,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 11\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 12,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 12\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 13,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 13\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 14,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 14\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 15,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 15\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 16,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 16\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 17,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 17\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 18,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 18\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 19,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 19\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 20,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 20\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 21,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 21\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 22,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 22\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 23,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 23\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 24,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 24\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 25,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 25\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 26,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 26\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 27,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 27\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 28,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 28\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 29,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 29\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 30,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 30\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 31,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 31\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 32,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 32\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 33,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 33\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 34,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 34\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 35,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 35\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 36,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 36\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 37,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 37\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 38,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 38\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 39,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 39\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 40,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 40\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 41,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 41\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 42,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 42\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 43,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 43\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 44,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 44\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 45,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 45\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 46,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 46\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 47,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"2\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 47\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"3\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 21,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"3\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 21\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"3\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 55,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"3\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 55\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"3\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 60,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"3\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 60\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"chargePeriod\": \"3\",\n" +
                "\t\t\t\t\t\t\"chargeYear\": 65,\n" +
                "\t\t\t\t\t\t\"coveragePeriod\": \"3\",\n" +
                "\t\t\t\t\t\t\"coverageYear\": 65\n" +
                "\t\t\t\t\t}\n" +
                "\t\t\t\t]";

        System.out.println( JSONUtil.parseArray(json).size()*3);

    }
    @Test
    public void ss(){
        DateTimeFormatter datePattern = DatePattern.UTC_SIMPLE_FORMAT.getDateTimeFormatter();

//        2024-08-26T06:43:27.897Z
        JSONObject jsonObject = new JSONObject();
        ZonedDateTime now = ZonedDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
        String formattedDateTime = now.format(formatter);
//        String format = LocalDateTimeUtil.format( LocalDateTime.now(), DatePattern.NORM_DATETIME_MS_PATTERN);
        String format =  LocalDateTimeUtil.format(LocalDateTime.now(),datePattern);
        jsonObject.put("timestamp",format);
        System.out.println(jsonObject);
    }
}
